package com.neusoft.jxc.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.neusoft.jxc.bean.Goods;
import com.neusoft.jxc.bean.GoodsType;
import com.neusoft.jxc.bean.Pager;
import com.neusoft.jxc.bean.Provider;
import com.neusoft.jxc.bean.Result;
import com.neusoft.jxc.service.GoodsService;
import com.neusoft.jxc.service.GoodsTypeService;
import com.neusoft.jxc.service.ProviderService;
import com.neusoft.jxc.util.FileResult;
import com.neusoft.jxc.util.FileuploadUtil;

/**
 * 
 * @author chens
 *
 */
@Controller
@RequestMapping("goods")
public class GoodsController {
	private Logger logger = Logger.getLogger(GoodsController.class);

	@Autowired
	private GoodsService goodsService;

	@Autowired
	private GoodsTypeService goodsTypeService;

	@Autowired
	private ProviderService providerService;

	@RequestMapping("list")
	@ResponseBody
	public Pager<Goods> list(Goods goods, @RequestParam(defaultValue = "1", value = "page") Integer page,
			@RequestParam(defaultValue = "10", value = "rows") Integer size) {

		logger.info(goods);

		Pager<Goods> pager = new Pager<Goods>(page, size);
		pager.setCondition(goods);

		List<Goods> list = goodsService.findList(pager);
		int total = goodsService.findTotal(pager);

		pager.setRows(list);
		pager.setTotal(total);

		return pager;
	}

	/**
	 * <pre>
	 * 设计一个方法，接收用户输入的商品信息，调用服务接口进行数据保存，根据新增结果返回操作结果给页面
	 *  spring mvc会把上传的文件封装到MultipartFile对象中，上传就是把数据从该对象保存到服务器的文件系统中
	 * </pre>
	 * 
	 * @param goods
	 * @return
	 */
	@RequestMapping("add")
	@ResponseBody
	public Result add(Goods goods, HttpServletRequest request, MultipartFile file1) {
		// 判断是否有文件需要上传
		if (file1 != null && !file1.isEmpty()) {
			FileResult fileResult = FileuploadUtil.uploadFile(request.getServletContext(), file1);
			if (!fileResult.getSuccess()) {
				return new Result(false, fileResult.getMsg());
			}
			String serverFileString = fileResult.getServerPath();
			goods.setMainImage(serverFileString);
		}

		Result result = new Result(Boolean.FALSE, "新增失败");

		boolean ret = goodsService.add(goods);
		if (ret) {
			result = new Result(Boolean.TRUE, "新增成功");
		}

		return result;
	}

	@RequestMapping("edit")
	@ResponseBody
	public Result edit(Goods goods, HttpServletRequest request, MultipartFile file1) {
		// 判断是否有文件需要上传
		if (file1 != null && !file1.isEmpty()) {
			FileResult fileResult = FileuploadUtil.uploadFile(request.getServletContext(), file1);
			if (!fileResult.getSuccess()) {
				return new Result(false, fileResult.getMsg());
			}
			String serverFileString = fileResult.getServerPath();
			goods.setMainImage(serverFileString);
		}

		Result result = new Result(Boolean.FALSE, "修改失败");

		boolean ret = goodsService.edit(goods);
		if (ret) {
			result = new Result(Boolean.TRUE, "修改成功");
		}

		return result;
	}

	@RequestMapping("delete")
	@ResponseBody
	public Result delete(Goods goods) {

		Result result = new Result(Boolean.FALSE, "删除失败");

		boolean ret = goodsService.deleteById(goods);
		if (ret) {
			result = new Result(Boolean.TRUE, "删除成功");
		}

		return result;
	}

	@RequestMapping("goodstypelist")
	@ResponseBody
	public List<GoodsType> goodsTypeList() {
		return goodsTypeService.findAll();
	}

	@RequestMapping("providerlist")
	@ResponseBody
	public List<Provider> providerList() {
		return providerService.findAll();
	}
}
